package cc.rengu.igas.cops.core.service.trans.merchant;

import cc.rengu.igas.bomp.facade.base.BaseResponse;
import cc.rengu.igas.bomp.facade.base.Header;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.igas.cops.common.constant.CopsParamConstant;
import cc.rengu.igas.cops.common.constant.CopsTreeNodeConstant;
import cc.rengu.igas.cops.common.enums.CopsToBompDubboServiceEnum;
import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.cops.core.model.CopsReflectMsgModel;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizHeader;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Method;

/**
 * 业务管理后台处理基类
 *
 * @author Xuegui Cao
 * 13 接口合 1
 */
public class MchntEntranceService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        try {
            callDubboService();
        } catch (Exception e) {
            rglog.error(e.getMessage());
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception ex) {
                rglog.error(e.getMessage());
            }
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    /**
     * 服务调用
     */
    private void callDubboService() throws Exception {
        /*获取APPID*/
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        xmlTreeUtil.setXmlTreeStringValue("appId", xmlTreeUtil.getXmlTreeStringValue(CopsTreeNodeConstant.DEFINE_HTTP_APPID));

        BizHeader bizHeader = new BizHeader();
        ConvertUtil.convertOutput(bizHeader);

        //交易配置
        CopsReflectMsgModel copsReflectMsgModel = null;
        try {
            SysParamService sysParamService = new SysParamServiceImpl();
            SysParam sysParam = sysParamService.getSysParamInfo(bizHeader.getInstId(), CopsParamConstant.COPS_REFLECT_PARAM, bizHeader.getTxnNum());
            if (null != sysParam) {
                copsReflectMsgModel = JSONObject.parseObject(sysParam.getParamValue(), CopsReflectMsgModel.class);
            }
        } catch (Exception e) {
            rglog.error("系统参数表配置<{}>交易码映射关系异常,尝试从枚举中获取映射关系.", bizHeader.getTxnNum());
        }
        if (null == copsReflectMsgModel) {
            //获取交易映射关系
            CopsToBompDubboServiceEnum copsToBompDubboServiceEnum = CopsToBompDubboServiceEnum.getEnumBycopsTxnNum(bizHeader.getTxnNum());
            if (CopsToBompDubboServiceEnum.NULL_ENUM == copsToBompDubboServiceEnum) {
                rglog.error("无交易映射关系,交易拒绝");
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            copsReflectMsgModel = JSONObject.parseObject(copsToBompDubboServiceEnum.getReflectMsgJsonString(), CopsReflectMsgModel.class);
        }
        if (null == copsReflectMsgModel) {
            rglog.error("无<{}>交易码映射关系,交易拒绝.", bizHeader.getTxnNum());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        //反射获取请求对象
        Class<?> requestClass = Class.forName(copsReflectMsgModel.getRequest());
        Object facadeRequest = requestClass.newInstance();
        ConvertUtil.convertOutput(facadeRequest);
        Method getHeaderMethod = requestClass.getMethod(CopsParamConstant.GET_HEADER);
        Header header = (Header) getHeaderMethod.invoke(facadeRequest);
        header.setTxnNum(copsReflectMsgModel.getTxnNum());

        //服务调用
        DubboService dubboService = new DubboServiceImpl();
        Object dubboResult = dubboService.callDubboService(copsReflectMsgModel.getDubboBean(), copsReflectMsgModel.getDubboMethod(), facadeRequest);
        Result result = (Result) dubboResult;
        if (null == dubboResult || !result.isSuccess()) {
            throw new BizException("调用Dubbo服务失败.");
        }
        BaseResponse baseResponse = (BaseResponse) result.getResult();
        // if (!RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(baseResponse.getRespCode())) {
        // }
        baseResponse.setRespDesc(baseResponse.getRespDesc() + bizHeader.getTraceNo());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, baseResponse.getRespDesc());
        ConvertUtil.convertInput(baseResponse);
    }

}
